import { ReactNode, useEffect, useState } from 'react'
type ErrorBoundaryOptions = {
    children: ReactNode;
    fallback: ReactNode;
}


const ErrorBoundary: React.FC<ErrorBoundaryOptions> = ({ children, fallback }) => {
    const [hasError, setHasError] = useState(false);

    useEffect(() => {
        const handleError = (err: ErrorEvent) => {
            setHasError(true);
            console.error('ErrorBoundary caught an error:', err);
        };

        window.addEventListener('error', handleError);

        return () => {
            window.removeEventListener('error', handleError);
        };
    }, []);

    if (hasError) {
        return <>{fallback}</>;
    }

    return <>{children}</>;
}
export default ErrorBoundary;